package com.tangyanbo.mycontacts;

import java.util.ArrayList;
import java.util.List;

import com.tangyanbo.mycontacts.activity.ContactView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class CantactListAdapter extends BaseAdapter{

	private List<ContactText> items = new ArrayList<ContactText>();
	
	private LayoutInflater mInflater;
	
	public CantactListAdapter(Context context){
		mInflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void addItem(ContactText contactText){
		items.add(contactText);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ContactTextViewHolder contactTextViewHolder;		 
		if (convertView == null) {
			contactTextViewHolder = new ContactTextViewHolder();		
			convertView = mInflater.inflate(R.layout.phone_list, null);
			contactTextViewHolder.nameView = (TextView) convertView.findViewById(R.id.name);
			contactTextViewHolder.numberView = (TextView) convertView.findViewById(R.id.number);
			contactTextViewHolder.cantactPhotoButton = (ImageButton) convertView.findViewById(R.id.cantactPhoto);
			convertView.setTag(contactTextViewHolder);
		}else{
			contactTextViewHolder = (ContactTextViewHolder) convertView.getTag();
		}
		contactTextViewHolder.nameView.setText(items.get(position).getDisplayName());
		contactTextViewHolder.numberView.setText(items.get(position).getNumber());
		contactTextViewHolder.cantactPhotoButton.setImageBitmap(items.get(position).getCantactPhoto());
//		convertView.setClickable(true);
//		convertView.setOnClickListener(new OnClickListener(){
//			@Override
//			public void onClick(View arg0) {
//				
//			}			
//		});
		return convertView;
	}

	public void setItems(List<ContactText> items) {
		this.items = items;
	}

}
